var fs = require("fs");

var filename = './2_fsOperation.txt';
var dir = '/home/c4dev/workspace';

console.log("1. Starting dir operation .....\n");
fs.stat(dir, (err, stats) => {
    console.log("1.1 Starting dir operation .....\n");
    if(err) return console.error(err);
    console.log(dir + " isDir ? " + stats.isDirectory());
    fs.readdir(dir + "/tmp/", (err, files) => {
        if(err) {
            console.log("dir doesn't exist!");
            fs.mkdir(dir + "/tmp/", function(err) {
                if(err) return console.error(err);
                console.log("dir create succeed!" + dir + "/tmp");
            });
        }
        fs.rmdir(dir + "/tmp/", (err) => {
            if(err) return console.error(err);
            console.log("Going to del directory " + dir + "/tmp");
        });
    });
    fs.readdir(dir, function(err, files) {
        if(err) return console.error(err);
        console.log("Files under :" + dir);
        console.log("=============================");
        files.forEach((file) => {
            console.log(file);
        });
        console.log("=============================");
    });
});

console.log("2. Starting file operation .....\n");
var buf = new Buffer(1024);
fs.access(filename, fs.constants.R_OK | fs.constants.W_OK, (err) => {
    console.log("2.2 Starting file operation .....\n");
    console.log(err ? 'no access!' : 'can read/write');
    if(err) {
        console.log("try to create a new file");
        fs.open(filename, 'w+', (err, fd) => {
            if(err) return console.error(err);
            fs.writeFile(filename, 
                "Li, Jeremy : Gold is a girl;" +
                "Li, Bob : Gold is a boy;" +
                "Li, Jim : Myth is fake;" +
                "Li, Simmon : Myth is amazing", function(err) {
                if(err) return console.log("write file error!");
                else 
                {
                    console.log("write succeed!");
                    fs.ftruncate(fd, 50, (err) => {
                        if(err) return console.error(err);
                        console.log("truncate file after 10 bytes");
                    });
                    fs.close(fd, (err) => { console.log("close file"); });
                }
            });
        });
        return 0;
    }
    else {
        fs.stat(filename, function(err, stats) {
            if(err) return console.error(err);
            console.log(stats);
            console.log("isFile ? " + stats.isFile());
            console.log("isDirectory ? " + stats.isDirectory());
        });
        fs.watchFile(filename, function (curr, prev) {
            console.log('the current mtime is: ' + curr.mtime);
            console.log('the previous mtime was: ' + prev.mtime);
            fs.unwatchFile(filename);
        });
        fs.open(filename, 'r+', (err, fd) => {
            fs.read(fd, buf, 0, buf.length, 0, function(err, bytes) {
                if(err) return console.error(err);
                console.log(bytes + " bytes read");
                if(bytes > 0) {
                    var text = buf.slice(0, bytes).toString()
                    console.log(text);
                    text.split(/;/).forEach(function(line) {
                        var item = line.split(/:/)
                        console.log("Author[" + item[0] + "] : " + item[1]);
                    });
                } 
                fs.close(fd, (err) => { console.log("close file"); });
            });
        });

        fs.unlink(filename, (err) => {
            if(err) {
                console.error(err);
            }
            else console.log("successfully delete ./2_fsOperation.txt");
        });
    }
});

